package com.restaurant.springboot.controller;

import com.restaurant.springboot.annotation.TokenAuth;
import com.restaurant.springboot.entity.ShoppingCartEntity;
import com.restaurant.springboot.entity.UserEntity;
import com.restaurant.springboot.service.ShopcartService;
import com.restaurant.springboot.utils.RespCode;
import com.restaurant.springboot.utils.RespEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author jack
 * @Date:2019/7/29 10:27
 */
@RestController
@RequestMapping("/api/shopcart")
public class ShopcartController {

    @Autowired
    private ShopcartService shopcartService;

    @Autowired
    CacheManager cacheManager;

    /**
     * 获取用户的所有购物车
     *
     * @return
     */
    @GetMapping("")
    @TokenAuth
    public RespEntity getAllShopcart(
            @RequestParam(value = "shareCode", required = false) Integer shareCode,
            HttpSession httpSession) {
        int userId = getUserId(shareCode, httpSession);
        if(userId == -1){
            return new RespEntity(
                    RespCode.NO_LOGIN
            );
        }else if(userId == -1){
            return new RespEntity(
                    500,
                    "shareCode Error"
            );
        }
        List list = shopcartService.getUserAllShopcartVo(userId);
        return new RespEntity(
                RespCode.SUCCESS,
                list
        );
    }

    private int getUserId(Integer shareCode, HttpSession httpSession) {
        int userId = 0;
        if (shareCode == null || shareCode == 0) {
            UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
            if (userEntity == null) {
                userId = -1 ;
            } else {
                userId = userEntity.getId();
            }
        } else {
            Cache shareCode1 = cacheManager.getCache("shareCode");
            Cache.ValueWrapper valueWrapper = shareCode1.get(shareCode);
            if (valueWrapper == null) {
                userId = -2 ;
            } else {
                userId = (int) valueWrapper.get();
            }
        }
        return userId;
    }

    @PostMapping("")
    @TokenAuth
    public RespEntity addShopcart(@RequestParam(value = "shareCode",required = false) Integer shareCode,@RequestBody ShoppingCartEntity shoppingCart, HttpSession httpSession) {
        int userId = getUserId(shareCode, httpSession);
        if(userId == -1){
            return new RespEntity(
                    RespCode.NO_LOGIN
            );
        }else if(userId == -1){
            return new RespEntity(
                    500,
                    "shareCode Error"
            );
        }
        shoppingCart.setUserId(userId);
        shoppingCart.setNumber(1);
        shopcartService.addShopcart(shoppingCart);
        return new RespEntity(
                RespCode.SUCCESS
        );
    }

    /**
     * 增加或者减少购物车数量
     *
     * @param shoppingCart 购物车信息
     * @param httpSession  上下文
     * @return 结果
     */
    @PutMapping("")
    @TokenAuth
    public RespEntity updateShopcart(@RequestParam(value = "shareCode",required = false)Integer shareCode,@RequestBody ShoppingCartEntity shoppingCart, HttpSession httpSession) {
        int userId = getUserId(shareCode, httpSession);
        if(userId == -1){
            return new RespEntity(
                    RespCode.NO_LOGIN
            );
        }else if(userId == -1){
            return new RespEntity(
                    500,
                    "shareCode Error"
            );
        }
        shoppingCart.setUserId(userId);
        if (shopcartService.updateShopcart(shoppingCart)) {
            return new RespEntity(RespCode.SUCCESS);
        } else {
            return new RespEntity(RespCode.ERROR);
        }
    }

    /**
     * 删除购物车内的商品
     *
     * @param shoppingCart 购物车
     * @param httpSession  上下文
     * @return 删除结果
     */
    @DeleteMapping("")
    @TokenAuth
    public RespEntity deleteShopcart(@RequestParam(value = "shareCode",required = false)Integer shareCode,@RequestBody ShoppingCartEntity shoppingCart, HttpSession httpSession) {
        int userId = getUserId(shareCode, httpSession);
        if(userId == -1){
            return new RespEntity(
                    RespCode.NO_LOGIN
            );
        }else if(userId == -1){
            return new RespEntity(
                    500,
                    "shareCode Error"
            );
        }
        shoppingCart.setUserId(userId);
        if (shopcartService.deleteShopcart(shoppingCart)) {
            return new RespEntity(RespCode.SUCCESS);
        } else {
            return new RespEntity(RespCode.ERROR);
        }
    }

    @PostMapping("/share")
    @TokenAuth
    public RespEntity getShare(HttpSession httpSession){
        int userId = 0;
        UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
        if (userEntity == null) {
            return new RespEntity(
                    RespCode.NO_LOGIN
            );
        } else {
            userId = userEntity.getId();
        }
        int code = (int)((Math.random()*10000000))%1000000;
        Cache shareCode = cacheManager.getCache("shareCode");
        shareCode.put(code, userId);
        return new RespEntity(
                RespCode.SUCCESS,
                code
        );
    }

}
